<?php

/* 
 * Plugin Name:   Feed Burning(FEED合烧)
 * Version:       1.0
 * Plugin URI:    http://blog.2i2j.com/
 * Description:   Feed Burning 是一个Feed合烧(聚合)工具, 可以聚合两个站点以上的Feed内容混合输出, 按时间按排序.
 * Author:        偶爱偶家
 * Author URI:    http://blog.2i2j.com/
 *
 * License:       GNU General
 */

if(!class_exists('feedburning')):
class feedburning{

	var $items = array();
	
	function dealfeed($items=array()){

		if(!isset($items) || empty($items) || !is_array($items))
			return false;

		$rss2_itemtags = array('title', 'link', 'description', 'pubdate', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubdate', 'source', 'content:encoded', 'dc:creator', 'wfw:commentrss');

		$result = array();
		
		if(empty($items)){
			return false;
		}

		if(is_array($items)){
			foreach($items as $item){
				if(isset($item['pubdate']) && !empty($item['pubdate'])){
					$key = intval(strtotime($item['pubdate']));
				}else{
					$key = intval(time());
				}

				$temp = array();

				foreach($rss2_itemtags as $tag){
					$tag = explode(':',$tag);
					if( isset($item[$tag[0]]) && !empty($item[$tag[0]]) ){
						if( isset($tag[1]) && isset($item[$tag[0]][$tag[1]]) && !empty($item[$tag[0]][$tag[1]]) ){
							$temp[$tag[0].':'.$tag[1]] = $item[$tag[0]][$tag[1]];
						}else{
							$temp[$tag[0]] = $item[$tag[0]];
						}
					}else{
						$temp[$tag[0]] = '';
					}
				}
				$result[$key][] = $temp;
			}
			return $result;
		}
	}

	function mergefeed($result=array(), $array=array()){
		if(!is_array($array)){
			return false;
		}

		foreach($array as $key => $val){
			while(array_key_exists($key, $result)){
				$key--;
			}

			$result[$key] = $val;
		}
		krsort($result);

		return $result;
	}

	function fetchfeed($url){

		if(empty($url)){
			return false;
		}

		if(!function_exists('_fetch_remote_file')){
			require_once(ABSPATH.WPINC.'/rss.php');
		}

		//$rss = fetch_rss($url);
		$resp = _fetch_remote_file($url);
		if($resp->status < 200 || $resp >= 300)
			return false;

		$rss = new MagpieRSS($resp->results);

		return $rss->items;
	}

	function dofeed(){
		ob_start();
		ob_clean();
        do_feed();
		$resp = ob_get_contents();
		ob_end_clean();

		if(!class_exists('MagpieRSS')){
			require_once(ABSPATH.WPINC.'/rss.php');
		}

		$rss = new MagpieRSS($resp);

		return $rss->items;
	}

	function getoption(){
		
	}

	function feedburning($urls){

		$rss = array();
		
		$rss = $this->dofeed();
	
		$rss = $this->dealfeed($rss);

		if($rss === false)
			exit;

		$this->items = $this->mergefeed($this->items,$rss);

		foreach($urls as $url){
			if(empty($url)){
				continue;
			}

			$rss = array();

			$rss = $this->fetchfeed($url);

			if($rss === false){
				continue;
			}

			$rss = $this->dealfeed($rss);

			if($rss === false){
				continue;
			}

			$this->items = $this->mergefeed($this->items,$rss);
		}

		if(!function_exists('createrss2')){
			require_once(dirname(__FILE__).'/rss-function.php');
		}

		if(is_array($this->items)){
			creatrss2($this->items);
		}
		exit;
	}
}
endif;

function feed_burning(){
	global $wp_query;

	$feed_burning_addr = get_option('feed_burning_addr');
	
	if( is_feed() && !empty($feed_burning_addr) && !$wp_query->is_comment_feed ){
		$urls = explode("\n",$feed_burning_addr);

		$feedburning = new feedburning($urls);
		exit;
	}else{
		return true;
	}
}

add_action('template_redirect', 'feed_burning');

function feed_burning_options(){
	$message = '';
	
	if (isset($_POST['feedburningupdate'])){
		$feed_burning_addr = !empty($_POST['feedburningaddr']) ? stripslashes($_POST['feedburningaddr']) : '';
	    
		if(update_option("feed_burning_addr",$feed_burning_addr))
			$message.=__('Configuration updated','feed_burning');
		else
			$message.=__('Update Fail','feed_burning');

		if($message!="") {
			echo '<div class="updated"><strong><p>'. $message . '</p></strong></div>';
		}
	}
	$feed_burning_addr = get_option("feed_burning_addr");
?>
<div class=wrap>
	<form method="post" action="">
		<h2><?php _e('Feed Burning','feed_burning') ?></h2>
		<fieldset name="wp_basic_options"  class="options">
			<textarea cols="50" rows="8" name="feedburningaddr" id="feedburningaddr"><?php echo $feed_burning_addr; ?></textarea>
			<br /><small>每行一个网址</small>
		</fieldset>
		<p class="submit"><input type="submit" name="feedburningupdate" value="<?php _e('Update Options &raquo;','feed_burning') ?>" /></p>
	</form>
</div>
<?php
}

function feed_burning_options_admin(){
	add_options_page(__('Feed Burning','feed_burning'), 'Feed Burning', 5,  __FILE__, 'feed_burning_options');
}

add_action('admin_menu', 'feed_burning_options_admin');

?>